package com.qiuzhiyi.activity;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.li.utils.TransformUtil;
import com.li.widget.TopNavigationBar;
import com.li.widget.VerticalDoubleText;
import com.qiuzhiyi.R;
import com.qiuzhiyi.adapter.ImageAdapter;
import com.qiuzhiyi.base.QZYBaseActivity;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import butterknife.BindView;
import butterknife.OnClick;

public class HuoShiActivity extends QZYBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    @BindView(R.id.txt_more)
    TextView mTxtMore;
    @BindView(R.id.layout_content)
    LinearLayout mLayoutContent;
    private long mClickTime = 0;
    private LinkedHashMap<String, String> mStringMap;
    private ImageAdapter mAdapter;
    private ArrayList<String> mPictureList;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_huo_shi;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);

        Bundle bundle = getIntent().getExtras();
        if (bundle == null) {
            return;
        }
        mStringMap = new LinkedHashMap<>();
        mStringMap.put("伙食：", bundle.getString("foodFee"));
        mStringMap.put("住宿:", bundle.getString("live"));
        mStringMap.put("其它:", bundle.getString("foodRemind"));
        mPictureList = bundle.getStringArrayList("foodPic");
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, -2);
        params.setMargins(0, 0, 0, TransformUtil.dp2px(mContext, 10));
        for (String s : mStringMap.keySet()) {
            VerticalDoubleText doubleText = new VerticalDoubleText(mContext);
            doubleText.setTitle(s);
            doubleText.setContent(mStringMap.get(s));
            doubleText.setPadding(TransformUtil.dp2px(mContext, 10), 0, 0, 0);
            mLayoutContent.addView(doubleText, params);
        }

        mRecycler.setLayoutManager(new GridLayoutManager(mContext, 3));
        mRecycler.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                GridLayoutManager manager = ((GridLayoutManager) parent.getLayoutManager());
                int spanCount = manager.getSpanCount();
                outRect.bottom = TransformUtil.dp2px(mContext, 5);
                if ((parent.getChildAdapterPosition(view) + 1) % spanCount == 0) {
                    outRect.right = TransformUtil.dp2px(mContext, 0);
                } else {
                    outRect.right = TransformUtil.dp2px(mContext, 5);
                }
            }
        });
        mAdapter = new ImageAdapter(R.layout.item_img);
        mAdapter.bindToRecyclerView(mRecycler);
        mAdapter.setNewData(mPictureList);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                long currentTime = System.currentTimeMillis();
                if (currentTime - mClickTime < 500) {
                    mClickTime = 0;
                    Bundle bundle = new Bundle();
                    bundle.putStringArrayList("0", mPictureList);
                    bundle.putInt("1", position);
                    startWithArg(ImageListActivity.class, bundle);
                } else {
                    mClickTime = currentTime;
                }
            }
        });
    }

    @OnClick(R.id.txt_more)
    public void onViewClicked() {
        if (mPictureList != null && mPictureList.size() > 0) {
            Bundle bundle = new Bundle();
            bundle.putStringArrayList("0", mPictureList);
            bundle.putInt("1", 0);
            startWithArg(ImageListActivity.class, bundle);
        } else {
            showToast("暂无图片");
        }
    }
}
